package com.tg.app.activity.account;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.appbase.custom.config.ApiUrl;
import com.appbase.custom.constant.CommonConstants;
import com.base.BaseActivity;
import com.tange.base.toolkit.AppUtil;
import com.tange.base.toolkit.PreferenceUtil;
import com.tange.base.toolkit.StringUtils;
import com.tange.core.backend.service.api.CoreApiUrl;
import com.tange.module.camera.hub.CameraHub;
import com.tange.module.core.wifi.scan.BizWiFiUtil;
import com.tange.module.log.collection.AppLogManager;
import com.tg.app.R;
import com.tg.app.activity.WebActivity;
import com.tg.app.helper.TGGlobalConfigHelper;
import com.tg.app.util.LogUtils;
import com.tg.app.util.ObjectBoxUtil;
import com.tg.app.util.UriUtil;
import com.tg.appcommon.android.LanguageUtils;
import com.tg.appcommon.android.TGLog;

public class ToolBoxActivity  extends BaseActivity implements View.OnClickListener {
    private final static String TAG = ToolBoxActivity.class.getSimpleName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        setContentView(R.layout.activity_tool_box);
        hideActionBar();
        initView();
        modifyToolBar();
    }

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    private void showPriceItem(){
        String country = LanguageUtils.getCountry(this);
        TGLog.d(TAG, "getCentigrade language = country = " + country);

        if (StringUtils.equalsIgnoreCase(country, "th")){
            findViewById(R.id.btn_toolbox_price).setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initView() {
        TextView title = findViewById(R.id.device_name);
        title.setText(R.string.account_toolbox);
        findViewById(R.id.back_toolbar).setOnClickListener(this);
        findViewById(R.id.btn_toolbox_local).setOnClickListener(this);
        findViewById(R.id.btn_toolbox_device_logs).setOnClickListener(this);
        View btnSim = findViewById(R.id.btn_toolbox_sim);
        btnSim.setOnClickListener(this);
        if (!LanguageUtils.isZh(this)){
            btnSim.setVisibility(View.GONE);
        }

        showPriceItem();
        findViewById(R.id.btn_toolbox_price).setOnClickListener(this);


        findViewById(R.id.btn_app_log).setVisibility(View.VISIBLE);
        findViewById(R.id.btn_app_log).setOnClickListener(v -> AppLogManager.getInstance().upload(ToolBoxActivity.this));
    }

    @Override
    protected void onResume() {
        super.onResume();
        CameraHub.getInstance().setEnableLocalApConnect(false);
    }

    @Override
    public void onClick(View view) {

        int id = view.getId();
        if (id == R.id.back_toolbar) {
            finish();
        }  else if (id == R.id.btn_toolbox_sim) {
            UriUtil.openServePlayImpl(this, new Intent(), TGGlobalConfigHelper.getInstance().getSim_check(), "", 0);
        } else if (id == R.id.btn_toolbox_price){
            Intent webIntent = new Intent(getBaseContext(), WebActivity.class);
            int userId = PreferenceUtil.getInt(this, CommonConstants.PRE_USER_ID);
            String price = TGGlobalConfigHelper.getInstance().getThaiLandServicePriceList();
            if (StringUtils.isEmpty(price)){
                price = ApiUrl.TOOLS_PRICE;
            }
            String url = String.format("%s%s?platform=android&version=%s&user_id=%d", CoreApiUrl.TG_STATIC, price, AppUtil.getVersionName(this), userId);
            webIntent.putExtra(WebActivity.KEY_WEBURL, url);
            webIntent.putExtra(WebActivity.KEY_WEBTITLE, getResources().getString(R.string.txt_price));
            startActivity(webIntent);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BizWiFiUtil.removeDeviceWifi();
    }
}
